package com.yushu.common.configuration.spring;

import com.yushu.common.util.date.DateUtils;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

import java.io.IOException;
import java.text.ParseException;
import java.util.Date;


 /**
 * spring json序列化
 * @author test
 * @CreateDate 2020-12-03 15:47:24
 */
public class SpringMVCCustomJsonDateDeserializer extends JsonDeserializer<Date> {

    @Override
    public Date deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {

        try {
            String dateString = jp.getText().trim();
            Date dateNew = DateUtils.stringToDate(dateString);
            return dateNew;
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
}